package com.code.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.code.app.R;

public class QQStepView extends View {

    private final String TAG = "StepView";

    private final int innerColor;
    private final int outerColor;
    private final int setpViewTextColor;
    private final int stepViewTextSize;
    private final int borderWidth;

    private Paint outerPaint;

    private int stepMax;
    private int currentStep;
    private Paint innerPaint;
    private Paint textPaint;

    public QQStepView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.QQStepView);
        innerColor = typedArray.getColor(R.styleable.QQStepView_innerColor, 0);
        outerColor = typedArray.getColor(R.styleable.QQStepView_outerColor, 0);
        setpViewTextColor = typedArray.getColor(R.styleable.QQStepView_setpViewTextColor, 0);
        stepViewTextSize = typedArray.getDimensionPixelSize(R.styleable.QQStepView_stepViewTextSize, 0);
        borderWidth = typedArray.getDimensionPixelSize(R.styleable.QQStepView_borderWidth, 0);
        typedArray.recycle();

        outerPaint = new Paint();
        outerPaint.setAntiAlias(true);
        outerPaint.setStrokeWidth(borderWidth);
        outerPaint.setColor(outerColor);
        //两端变圆弧
        outerPaint.setStrokeCap(Paint.Cap.ROUND);
        outerPaint.setStyle(Paint.Style.STROKE);

        innerPaint = new Paint();
        innerPaint.setAntiAlias(true);
        innerPaint.setStrokeWidth(borderWidth);
        innerPaint.setColor(innerColor);
        //两端变圆弧
        innerPaint.setStrokeCap(Paint.Cap.ROUND);
        innerPaint.setStyle(Paint.Style.STROKE);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(setpViewTextColor);
        textPaint.setTextSize(stepViewTextSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //MeasureSpec.AT_MOST 在布局中指定 wrap_content
        //MeasureSpec.EXACTLY 在布局中指定确定的值 100dp 或者使用 match_parent
        //MeasureSpec.UNSPECIFIED 尽可能大 但是不常用  NewListView ScorllView
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(Math.min(width, height), Math.min(width, height));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        RectF rectF = new RectF(borderWidth / 2, borderWidth / 2, getWidth() - borderWidth / 2, getHeight() - borderWidth / 2);
        canvas.drawArc(rectF, 135, 270, false, outerPaint);

        if (stepMax <= 0) {
            return;
        }
        if (currentStep > stepMax) {
            currentStep = stepMax;
        }
        if (currentStep < 0) {
            currentStep = 0;
        }
        float sweepAngle = (float)currentStep / stepMax;
        canvas.drawArc(rectF, 135, sweepAngle * 270, false, innerPaint);

        String text = currentStep+"";
        Rect rect = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), rect);
        int dx = getWidth() / 2 - rect.width() / 2;

        Paint.FontMetricsInt fontMetricsInt = textPaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
        int baseLine = getHeight() / 2 + dy;

        canvas.drawText(text, dx, baseLine, textPaint);
    }

    //其他，动画效果等
    public synchronized void setStepMax(int stepMax) {
        this.stepMax = stepMax;
    }

    //    synchronized,防止多线程操作出错
    public synchronized void setCurrentStep(int currentStep) {
        this.currentStep = currentStep;
        //不断绘制
        invalidate();
    }
}
